#include "at91sam7s.h"
#include "devices.h"
#include "board.h"

void spi_init(void)
{
    AT91PMC *pmc = AT91PMC_ADDR;
    AT91SPI *spi = AT91SPI_ADDR;
    unsigned n;

    pmc->PCER = 1 << PID_SPI;
    for(n = 0; n < 1000; n++);
    
    spi->CR = SPI_SPIEN | SPI_SWRST;
    spi->CR = SPI_SPIEN;
    
    spi->CSR0 = (8 << 8);
    spi->MR = SPI_MSTR | SPI_PS | SPI_PCS0;
}

void spi_xmit(unsigned val)
{
    AT91SPI *spi = AT91SPI_ADDR;
    while(!(spi->SR & SPI_TXEMPTY));
    spi->TDR = (val & 0xff) | SPI_LASTXFER;
}

